package servlet.user;



import entity.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/user/login")
public class UserLoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        User user = userService.login(userName, password);
        if (user != null) {
            request.getSession().setAttribute("user", user);
            response.setContentType("application/json");
            response.getWriter().write("{ \"code\": 100 }");
        } else {
            response.setContentType("application/json");
            response.getWriter().write("{ \"code\": 101 }");
        }
    }


    }
